package com.net.model.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.drug.lib.view.LoadingDialog;

import me.yokeyword.fragmentation.SupportFragment;

public abstract class BaseVBFragment<T extends ViewBinding> extends SupportFragment {
    protected T mBinding;
    protected Context mContext;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mBinding = getViewBinding();
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initialize();
        initData();
        createLoadDialog();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        requestNetwork();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBinding = null;
    }

    /**
     * 获取加载布局文件
     */
    protected abstract T getViewBinding();


    /**
     * 初始化数据
     */
    protected abstract void initialize();

    protected abstract void initData();

    protected abstract void requestNetwork();

    LoadingDialog loadingDialog;

    public void createLoadDialog() {
        LoadingDialog.Builder builder = new LoadingDialog.Builder(getContext())
                .setMessage("加载中...")
                .setCancelOutside(false)
                .setCancelable(false);
        loadingDialog = builder.create();
    }

    /**
     * description: 加载动画消失
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:15
     * @return: void
     */

    public void dismissDialog() {
        if (loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

    /**
     * description: 加载动画显示
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:16
     * @return: void
     */

    public void showLoading() {
        if (!loadingDialog.isShowing())
            loadingDialog.show();
    }

    /**
     * description: 隐藏软键盘
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:18
     * @Param view:
     * @return: void
     */

    protected void hideSoftInput(EditText view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        mInputMethodManager
                .hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * description: 打开软键盘
     *
     * @author: zhouhequan
     * @date: 2021/10/25 11:19
     * @Param view:
     * @return: void
     */

    protected void showSoftInput(EditText view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        mInputMethodManager
                .showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    private Handler handler;

    protected final Handler getHandler() {
        if (handler == null) {
            handler = new Handler(mContext.getMainLooper());
        }
        return handler;
    }

}
